#include "marketData.hpp"
#include<iostream>
#include <fstream>
#include <mutex>
#include <map>
#include "config.hpp"
#pragma once
#pragma warning(disable:4996)

static class txtUtility {
private:
	struct txtMarketData
	{
		char stkcd[15];
		char tdate[15];
		char ttime[15];
		double cp;
		double sellprice[10] = { 0 };
		double buyprice[10] = { 0 };
		double sellvolume[10] = { 0 };
		double buyvolume[10] = { 0 };
		double hp;
		double lp;
		double HighLimit;
		double LowLimit;
		double tt;
		double ts;
		double OPENPRC;
		double PRECLOSE;
		double transactions_count;
		double weightedAvgBidPRC;
		double weightedAvgAskPRC;
		double total_bid_size;
		double total_ask_size;
		double peration1;
		double peration2;
	};
public:
	

	static int char2int(char* mychar,int start,int end) {
		int myint = 0;
		for (int i = start; i <= end; i++) {
			myint = myint * 10 + (int)mychar[i] - 48;
		}
		return myint;
	}

	static int getLocalTime() {

	}


	static std::vector<marketData> getDataFromTxt(std::string path,std::string code, std::string today) {
		//today.erase(std::remove(today.begin(), today.end(), '-'), today.end());
		//std::string fileName = "/home/maoheng/marketData/" + today + '/' + code + ".txt";
		std::vector<marketData> data;
		std::string fileName =path+ today + "/" + code + ".txt";
	
		txtMarketData stock;
		marketData mydata;
		int n = 0;
		FILE* fp;
		fp = fopen(fileName.c_str(), "r");
		if (!fp) {
			return data;
		}
		while (!feof(fp)) {
			fscanf(fp, "%15[^,],%15[^,],%15[^,],%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", stock.stkcd, stock.tdate, stock.ttime, &stock.cp, &stock.sellprice[0], &stock.sellprice[1], &stock.sellprice[2], &stock.sellprice[3], &stock.sellprice[4], &stock.sellprice[5], &stock.sellprice[6], &stock.sellprice[7], &stock.sellprice[8], &stock.sellprice[9], &stock.buyprice[0], &stock.buyprice[1], &stock.buyprice[2], &stock.buyprice[3], &stock.buyprice[4], &stock.buyprice[5], &stock.buyprice[6], &stock.buyprice[7], &stock.buyprice[8], &stock.buyprice[9], &stock.sellvolume[0], &stock.sellvolume[1], &stock.sellvolume[2], &stock.sellvolume[3], &stock.sellvolume[4], &stock.sellvolume[5], &stock.sellvolume[6], &stock.sellvolume[7], &stock.sellvolume[8], &stock.sellvolume[9], &stock.buyvolume[0], &stock.buyvolume[1], &stock.buyvolume[2], &stock.buyvolume[3], &stock.buyvolume[4], &stock.buyvolume[5], &stock.buyvolume[6], &stock.buyvolume[7], &stock.buyvolume[8], &stock.buyvolume[9], &stock.hp, &stock.lp, &stock.HighLimit, &stock.LowLimit, &stock.ts, &stock.tt, &stock.OPENPRC, &stock.PRECLOSE, &stock.transactions_count, &stock.weightedAvgBidPRC, &stock.weightedAvgAskPRC, &stock.total_bid_size, &stock.total_ask_size, &stock.peration1, &stock.peration2);
			/*for (int i = 0; i < 9; i++) {
				mydata.code[i] = stock.stkcd[i];
			}*/
			
			/*for (int i = 0; i < 9; i++) {
				mydata.date[i] = stock.tdate[i];
			}

			for (int i = 0; i < 6; i++) {
				mydata.tick = mydata.tick * 10 + (int)stock.ttime[i] - 48;
			}*/
			mydata.code = char2int(stock.stkcd, 0, 5);
			mydata.date = char2int(stock.tdate, 0, 7);
			mydata.tick = char2int(stock.ttime, 0, 8);

			mydata.lastPrice = stock.cp;
			mydata.preClosePrice = stock.PRECLOSE;
			mydata.volume = stock.ts;
			mydata.amount = stock.tt;
			if (stock.stkcd[9] == 'H') {
				mydata.amount = round(mydata.amount);
			}
			else {
				mydata.amount = floor(mydata.amount);
			}
			mydata.highestPrice = stock.hp;
			mydata.lowestPrice = stock.lp;
			mydata.upperLimitPrice = stock.HighLimit;
			mydata.lowerLimitPrice = stock.LowLimit;
			mydata.openPrice = stock.OPENPRC;
			for (int i = 0; i < 10; i++) {
				mydata.askPrice[i] = stock.sellprice[i];
				mydata.askVolume[i] = stock.sellvolume[i];
				mydata.bidPrice[i] = stock.buyprice[i];
				mydata.bidVolume[i] = stock.buyvolume[i];
			}
			data.push_back(mydata);
			n++;
		}
		fclose(fp);

		return data;
	}

	//static void saveDataToTxt(std::string code, std::string today, std::vector<double> data) {
	//	today.erase(std::remove(today.begin(), today.end(), '-'), today.end());
	//	std::string pathStr = "/home/maoheng/features/" + today;
	//	std::string fileName = pathStr + '/' + code + ".txt";
	//	if (access(pathStr.c_str(), 0) == -1) {
	//		std::filesystem::create_directories(pathStr);
	//	}
	//	std::ofstream OutFile(fileName, std::ios::out | std::ios::app);
	//	OutFile.setf(std::ios::fixed, std::ios::floatfield);
	//	//OutFile.precision(8);
	//	for (int i = 0; i < data.size(); i++) {
	//		if (i < data.size() - 1) {
	//			OutFile << data[i] << ',';
	//		}
	//		else {
	//			OutFile << data[i] << std::endl;
	//		}
	//	}
	//	OutFile.close();
	//}
};

static class configUtility {
public:
	static std::map<int,std::vector<config>> getParameters() {
		std::map<int, std::vector<config>> configMap;
		FILE* fp;

		fp = std::fopen("stock.csv", "r");

		if (!fp) {
			std::cout << "error!No config file!" << std::endl;
			return configMap;
		}

		while (!feof(fp)) {

			config conf;
			int code;
			fscanf(fp, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n");
			
		}
		fclose(fp);
		
		return configMap;
	}
};





